package com.example.musicplayer.activity;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;

import com.example.musicplayer.R;
import com.example.musicplayer.settings.MusicPlayerSettings;
import com.example.musicplayer.settings.SettingsItem;
import com.example.musicplayer.view.SettingsView;

public class BackgroundSettingsActivity extends BaseSettingsActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initSettingsItem();
        //update the image
        updateImageView(getSettingsView(SettingsItem.PLAY_SCREEN_IMAGE));
        updateImageView(getSettingsView(SettingsItem.MUSIC_PLAY_LIST_IMAGE));
    }

    @Override
    protected int setActivityTitle() {
        return R.string.title_background_settings_activity;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == SelectMusicMenuCoverActivity.RESULT_CODE && data != null
                && data.getBooleanExtra("result", true)) {
            int mode = data.getIntExtra(SelectMusicMenuCoverActivity.SELECT_MODE, 0);
            if (mode == SelectMusicMenuCoverActivity.MODE_PLAY_SCREEN) {
                updateImageView(getSettingsView(SettingsItem.PLAY_SCREEN_IMAGE));
            }
            if (mode == SelectMusicMenuCoverActivity.MODE_MUSIC_PLAY_LIST) {
                updateImageView(getSettingsView(SettingsItem.MUSIC_PLAY_LIST_IMAGE));
            }
        }
    }

    private void initSettingsItem() {
        setSettingsItem(SettingsItem.PLAY_SCREEN_BACKGROUND);
        setSettingsItem(SettingsItem.PLAY_SCREEN_IMAGE);
        setSettingsDivideItem();
        setSettingsEmptyItem((int) getResources().getDimension(R.dimen.bg_set_activity_empty_view));
        setSettingsItem(SettingsItem.MUSIC_PLAY_LIST_BACKGROUND);
        setSettingsItem(SettingsItem.MUSIC_PLAY_LIST_IMAGE);
        setSettingsDivideItem();
    }

    private void updateImageView(SettingsView settingsView) {
        if (settingsView.getSettingsItem() == SettingsItem.PLAY_SCREEN_IMAGE) {
            settingsView.setDescription(MusicPlayerSettings.getPlayScreenImageBitmap());
        } else if (settingsView.getSettingsItem() == SettingsItem.MUSIC_PLAY_LIST_IMAGE) {
            settingsView.setDescription(MusicPlayerSettings.getMusicPlayListImageBitmap());
        }
    }

    @Override
    protected void updateSettingsItem(SettingsView settingsView) {
        super.updateSettingsItem(settingsView);
        switch (settingsView.getSettingsItem()) {
            case SettingsItem.PLAY_SCREEN_BACKGROUND:
                settingsView.setSwitchChecked(MusicPlayerSettings.isSetPlayScreenBg());
                settingsView.setSummary(settingsView.isSwitchChecked() ?
                        getString(R.string.follow_selection) : getString(R.string.background_default));
                break;
            case SettingsItem.PLAY_SCREEN_IMAGE:
                settingsView.setVisible(MusicPlayerSettings.isSetPlayScreenBg());
                break;
            case SettingsItem.MUSIC_PLAY_LIST_BACKGROUND:
                settingsView.setSwitchChecked(MusicPlayerSettings.isSetMusicPlayListBg());
                settingsView.setSummary(settingsView.isSwitchChecked() ?
                        getString(R.string.follow_selection) : getString(R.string.background_default));
                break;
            case SettingsItem.MUSIC_PLAY_LIST_IMAGE:
                settingsView.setVisible(MusicPlayerSettings.isSetMusicPlayListBg());
                break;
        }
    }

    @Override
    protected void onSettingsItemClick(SettingsView settingsView, boolean isChecked) {
        super.onSettingsItemClick(settingsView, isChecked);
        SettingsView temp;
        switch (settingsView.getSettingsItem()) {
            case SettingsItem.PLAY_SCREEN_BACKGROUND:
                MusicPlayerSettings.setSetPlayScreenBg(isChecked);
                settingsView.setSummary(isChecked ?
                        getString(R.string.follow_selection) : getString(R.string.background_default));
                temp = getSettingsView(SettingsItem.PLAY_SCREEN_IMAGE);
                if (temp != null) {
                    temp.showOrHide(isChecked);
                }
                break;
            case SettingsItem.MUSIC_PLAY_LIST_BACKGROUND:
                MusicPlayerSettings.setSetMusicPlayListBg(isChecked);
                settingsView.setSummary(isChecked ?
                        getString(R.string.follow_selection) : getString(R.string.background_default));
                temp = getSettingsView(SettingsItem.MUSIC_PLAY_LIST_IMAGE);
                if (temp != null) {
                    temp.showOrHide(isChecked);
                }
                break;
            case SettingsItem.PLAY_SCREEN_IMAGE:
                Intent intent_1 = new Intent(this, SelectMusicMenuCoverActivity.class);
                intent_1.putExtra(SelectMusicMenuCoverActivity.SELECT_MODE, SelectMusicMenuCoverActivity.MODE_PLAY_SCREEN);
                startActivityForResult(intent_1, 1);
                break;
            case SettingsItem.MUSIC_PLAY_LIST_IMAGE:
                Intent intent_2 = new Intent(this, SelectMusicMenuCoverActivity.class);
                intent_2.putExtra(SelectMusicMenuCoverActivity.SELECT_MODE, SelectMusicMenuCoverActivity.MODE_MUSIC_PLAY_LIST);
                startActivityForResult(intent_2, 1);
                break;
        }
    }
}